package com.spm.librarianmanagersystem.controller;

import com.alibaba.fastjson.JSONArray;
import com.spm.librarianmanagersystem.pojo.Fine;
import com.spm.librarianmanagersystem.service.FineService;
import com.spm.librarianmanagersystem.utils.JsonResult;
import com.spm.librarianmanagersystem.utils.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

@RestController
public class FineController {
    @Autowired
    FineService service;

    @GetMapping("/api/admin/fine/detail")
    public JsonResult<JSONArray> getFinesAdminDetail() {
        return new JsonResult<>(service.getFinesAdminDetail());
    }

    @PostMapping("/api/user/fine/pay")
    public JsonResult payFine(@RequestBody Map<String, String> fineMessage){
//        return new JsonResult(service.updateFine0(borrow_id));
        Fine fine = new Fine();
        fine.setBorrowId(Integer.parseInt(fineMessage.get("borrow_id")));
        fine.setUserId(Integer.parseInt(fineMessage.get("user_id")));
        fine.setStatus(0);
        fine.setAmount(Double.valueOf(fineMessage.get("amount")));
        Date date = new Date(System.currentTimeMillis());
        fine.setPaymentTime(date);

        return new JsonResult(service.addFine(fine));
    }

    @GetMapping("/api/user/fine/history")
    public JsonResult<JSONArray> payFineHistory(){
        String currentUserId = UserContextHolder.getCurrentUserId();
        return new JsonResult<>(service.getFineHistory(currentUserId));
    }
}
